package com.linksfield.topup.controller.ordermanage;

import com.alibaba.fastjson.JSON;
import com.linksfield.topup.config.dto.ResultDataDto;
import com.linksfield.topup.config.dto.StringOutputConstant;
import com.linksfield.topup.entity.interfaces.ordermanage.details.request.RequestOrderDetails;
import com.linksfield.topup.entity.interfaces.ordermanage.details.response.ResponseOrderDetails;
import com.linksfield.topup.entity.interfaces.ordermanage.orderexport.request.RequestOrderExport;
import com.linksfield.topup.entity.interfaces.ordermanage.search.request.RequestOrderSearch;
import com.linksfield.topup.entity.interfaces.ordermanage.search.response.ResponseOrderSearch;
import com.linksfield.topup.service.OrderService;
import com.linksfield.topup.utils.ExcelUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 订单管理控制器类
 *
 * @author: lm
 * @date: 2019-11-26 15:30
 */
@RestController
@Api(value = "订单管理")

public class OrderManageController {
    private static Logger logger = Logger.getLogger(OrderManageController.class);


    @Autowired
    OrderService orderService;

    @PostMapping(value = "orderSearch", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "订单管理：搜索", response = ResponseOrderSearch.class)
    public ResultDataDto orderSearch(@ApiParam @RequestBody RequestOrderSearch request,
                                     @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【订单管理搜索】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return orderService.search(request, token);

    }


    @PostMapping(value = "orderDetails", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "订单管理：详情", response = ResponseOrderDetails.class)
    public ResultDataDto orderDetails(@ApiParam @RequestBody RequestOrderDetails request,
                                      @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【订单管理详情】请求：" + JSON.toJSONString(request));
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        return orderService.orderDetails(request, token);

    }

    @GetMapping("orderExport")
    @ApiOperation(value = "订单管理：导出")
    public ResultDataDto orderExport(@RequestParam(value = "partnerCode", required = false) String partnerCode,
                                     @RequestParam(value = "startDate", required = false) String startDate,
                                     @RequestParam(value = "endDate", required = false) String endDate,
                                     @RequestParam(value = "options", required = false) String options,
                                     @RequestParam(value = "token", required = false) String token,
                                     HttpServletResponse response, HttpServletRequest request) {
        logger.info("【订单管理:导出】请求：" + request.getRequestURL() + ";导出者：" + partnerCode);

        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }
        try {
            RequestOrderExport re = new RequestOrderExport();
            if (StringUtils.isNotBlank(partnerCode)) {
                re.setPartnerCode(URLDecoder.decode(partnerCode, "UTF-8"));
            }
            re.setStartDate(URLDecoder.decode(startDate, "UTF-8"));
            re.setEndDate(URLDecoder.decode(endDate, "UTF-8"));
            re.setOptions(URLDecoder.decode(options, "UTF-8"));
            return orderService.orderExport(re, token, response);
        } catch (Exception e) {
            logger.info("请求参数转码失败：" + e);
            return ResultDataDto.addOperationFailure("请求时转码失败！");
        }
    }

    @PostMapping(value = "refund", produces = "application/json;charset=utf-8")
    @ApiOperation(value = "确认退款")
    public ResultDataDto refund(@ApiParam @RequestBody RequestOrderDetails request,
                                @RequestHeader(name = "x-access-token", required = false) String token) {
        logger.info("【订单管理:确认退款】请求：" + request.getPayId());
        if (StringUtils.isBlank(token)) {
            return ResultDataDto.addOperationFailure(StringOutputConstant.CODE_TOKEN_EMPTY);
        }

        return orderService.refund(request, token);
    }


}
